﻿Imports System.Data

Partial Class CustomControls_Municipio
    Inherits System.Web.UI.UserControl
    Private _Requerido As Boolean
    Public Property Requerido() As Boolean
        Get
            Return _Requerido
        End Get
        Set(ByVal value As Boolean)
            _Requerido = value
        End Set
    End Property
    Public Property _ddlDepartamento() As DropDownList
        Get
            Return ddlDepto
        End Get
        Set(ByVal value As DropDownList)
            ddlDepto = value
        End Set
    End Property
    Public Property _ddlMun() As DropDownList
        Get
            Return ddlMunicipio
        End Get
        Set(ByVal value As DropDownList)
            ddlMunicipio = value
        End Set
    End Property
    Public Property _RequiredValidador() As RequiredFieldValidator
        Get
            Return RequiredFieldValidator1
        End Get
        Set(ByVal value As RequiredFieldValidator)
            RequiredFieldValidator1 = value
        End Set
    End Property

    Public Sub cargarMunicipio(ByVal codmunicio As String)
        'llenar el combo de departamentos (llenar combos)
        If ddlDepto.Items.Count < 1 Then
            llenarDepartamentos()
        End If
        Using cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
            cnn.Open()
            Try
                Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("usp_MunicipiosDeptoSelect", cnn)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@CodMunicipio", codmunicio)
                    Using drDatos As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader
                        If drDatos.HasRows Then
                            drDatos.Read()
                            ddlDepto.SelectedValue = drDatos("coddepto").ToString
                        End If
                        drDatos.NextResult()
                        If drDatos.HasRows Then
                            Me.ddlMunicipio.DataSource = drDatos
                            Me.ddlMunicipio.DataValueField = "Codmunicipio"
                            Me.ddlMunicipio.DataTextField = "municipio"
                            Me.ddlMunicipio.DataBind()
                            Me.ddlMunicipio.Items.Insert(0, "")
                            Me.ddlMunicipio.SelectedValue = codmunicio
                        End If
                    End Using
                End Using
            Catch ex As Exception
                Response.Write("Ha ocurrido un error: " & ex.ToString())
            Finally
                cnn.Close()
            End Try
        End Using
        'seleccionar el departamento
        'llenar el combo de municipios dependiendo del departamento seleccionado
        'seleccionar el municipio
    End Sub
    Public Sub llenarDepartamentos()
        Using cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
            cnn.Open()
            Try
                Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("select coddepto,departamento from departamentos", cnn)
                    cmd.CommandType = CommandType.Text
                    Using drDatos As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader
                        If drDatos.HasRows Then
                            Me.ddlDepto.DataSource = drDatos
                            Me.ddlDepto.DataValueField = "Coddepto"
                            Me.ddlDepto.DataTextField = "Departamento"
                            Me.ddlDepto.DataBind()
                            Me.ddlDepto.Items.Insert(0, "")
                        End If
                    End Using
                End Using
            Catch ex As Exception
                Response.Write("Ha ocurrido un error cargando los datos: " & ex.ToString())
            Finally
                cnn.Close()
            End Try
        End Using
    End Sub

    Protected Sub ddlDepto_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepto.SelectedIndexChanged
        Dim CodDepto As String
        If String.IsNullOrEmpty(ddlDepto.SelectedValue) Then
            Me.ddlMunicipio.Items.Clear()
            Exit Sub
        Else
            CodDepto = ddlDepto.SelectedValue
        End If
        Using cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
            cnn.Open()
            Try
                Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("usp_MunicipiosSelectByDepto", cnn)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@CodDepto", CodDepto)
                    Using drDatos As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader
                        If drDatos.HasRows Then
                            Me.ddlMunicipio.DataSource = drDatos
                            Me.ddlMunicipio.DataValueField = "CodMunicipio"
                            Me.ddlMunicipio.DataTextField = "Municipio"
                            Me.ddlMunicipio.DataBind()
                            Me.ddlMunicipio.Items.Insert(0, "")
                        End If
                    End Using
                End Using
            Catch ex As Exception
                MyError.mostrarError("Ha ocurrido un error cargando los datos: " & ex.Message.ToString(), Page)
            Finally
                cnn.Close()
            End Try
        End Using
    End Sub
    Public ReadOnly Property CodMunicipio() As String
        Get
            Return ddlMunicipio.SelectedValue
        End Get
    End Property
    Public ReadOnly Property CodDepto() As String
        Get
            Return ddlDepto.SelectedValue
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            Me.llenarDepartamentos()
        End If
    End Sub
    Public Sub limpiar()
        Me.ddlDepto.SelectedIndex = -1
        Me.ddlMunicipio.SelectedIndex = -1
    End Sub

End Class